﻿using Microsoft.Xna.Framework;
using ShevaEngine.Core.Interfaces;
using ShevaEngine.Core.Modules.Graphics;
using ShevaEngine.Core.Modules.Graphics.Cameras;
using ShevaEngine.Core.Modules.Materials;

namespace ShevaEngine.Core.Foundation.GraphicsObjects
{
    /// <summary>
    /// Box class.
    /// </summary>
    public class Box : GraphicsPrimitive
    {
        /// <summary>Radius.</summary>
        public Vector3 Radius;
        /// <summary>Color.</summary>
        public Color Color;

        /// <summary>DrawCallKey.</summary>
        private DrawCallKey DrawCallKey;
        /// <summary>Draw call.</summary>
        private DrawCall DrawCall;
        /// <summary>Material parameters.</summary>
        private GraphicsMaterialParametersList MaterialParameters;

        /// <summary>
        /// Constructor.
        /// </summary>
        public Box()
            : base()
        {
            this.MaterialParameters = new GraphicsMaterialParametersList();
            this.MaterialParameters.Add(new GraphicsMaterialParameterColor("Color", this.Color));
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="startVertex"></param>
        /// <param name="endVertex"></param>
        /// <param name="color"></param>
        public Box(Matrix transform, Vector3 radius, Color color)
            : this()
        {
            this.Transform = transform;
            this.Radius = radius;
            this.Color = color;

            this.DrawCall = Primitives.Instance.CreateBoxBorder();
            this.DrawCallKey = new DrawCallKey();

            (this.MaterialParameters[0] as GraphicsMaterialParameterColor).Color = this.Color;
            this.DrawCallKey.MaterialParamsID =
                MaterialManager.Instance[this.DrawCallKey.MaterialID].GraphicsProfile.GetMaterialParametersId(this.MaterialParameters);
        }

        /// <summary>
        /// Add in rendering pipeline.
        /// </summary>
        /// <param name="drawCallKey"></param>
        /// <param name="drawCall"></param>
        public override void AddInRenderingPipeline(RenderingPipeline renderingPipeline, Camera camera)
        {
            renderingPipeline.AddOpaqueGraphicsObject(this.DrawCallKey, this.DrawCall, Matrix.CreateScale(this.Radius) * this.Transform);
        }

        /// <summary>
        /// Add in rendering pipeline.
        /// </summary>
        /// <param name="renderingPipeline"></param>
        /// <param name="position"></param>
        /// <param name="distanceSquared"></param>
        public override void AddInRenderingPipeline(RenderingPipeline renderingPipeline, Vector3 position, float distanceSquared)
        {
            renderingPipeline.AddOpaqueGraphicsObject(this.DrawCallKey, this.DrawCall, Matrix.CreateScale(this.Radius) * this.Transform);
        }
    }
}
